﻿using System.Collections.Generic;
using Felbot.IO;

namespace Felbot.Profiles {
    /// <summary>This class represents a profile, each an individual connection to Battle.net.</summary>
    public class Profile {
        private string m_Name;
        private bool m_Open;
        private INIControl m_IniParser;

        /// <summary>
        /// Gets or sets whether the profile is open.
        /// </summary>
        public bool Open {
            get {
                return m_Open;
            }
            set {
                m_Open = value;
            }
        }

        /// <summary>
        /// Gets or sets the INIControl object used by this class to access the INI file associated with this class. Please use the Profiles.GetSetting and Profiles.SetSetting functions to access this.
        /// </summary>
        public INIControl IniParser {
            get {
                return m_IniParser;
            }
        }

        /// <summary>
        /// Gets or sets the name of the profile.
        /// </summary>
        public string ProfileName {
            get {
                return m_Name;
            }
            set {
                m_IniParser.Path = Program.ApplicationFolder.ToString() + "\\" + value + ".fbp";
                m_Name = value;
            }
        }

        /// <summary>Creates a new profile.</summary>
        /// <param name="a_sName">The name of the profile.</param>
        /// <param name="a_pGlobal">If not the global profile, provide the global profile here.</param>
        public Profile(string name, Profile global) {
            m_Name = name;
            if (global == null) //this is the global profile
            {
                m_IniParser = new INIControl(Program.ApplicationFolder + "\\" + name.ToLower() + ".fbp", null);
            } else {
                m_IniParser = new INIControl(Program.ApplicationFolder + "\\" + name.ToLower() + ".fbp", global.IniParser);
            }
        }

        /// <summary>
        /// Sets a profile setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <param name="a_sNewValue">The new INI value.</param>
        /// <param name="a_sOverwrite">True to overwrite whether it exists or not, false to only write if it does not exist.</param>
        /// <returns>Returns true if set, false if not set.</returns>
        public bool SetSetting(string section, string setting, Setting newValue, bool overwrite) {
            return m_IniParser.SetSetting(section, setting, newValue, overwrite);
        }

        /// <summary>
        /// Sets a profile setting, overwriting any previous setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <param name="a_sNewValue">The new INI value.</param>
        /// <returns>Returns true if set, false if not set.</returns>
        public bool SetSetting(string section, string setting, Setting newValue) {
            return SetSetting(section, setting, newValue, true);
        }

        /// <summary>
        /// Sets a profile setting, overwriting any previous setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <param name="a_sNewValue">The new INI value.</param>
        /// <returns>Returns true if set, false if not set.</returns>
        public bool SetSetting(string section, string setting, string newValue) {
            return SetSetting(section, setting, Setting.FromString(newValue));
        }

        /// <summary>
        /// Sets a profile setting, overwriting any previous setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <param name="a_bNewValue">The new INI value.</param>
        /// <returns>Returns true if set, false if not set.</returns>
        public bool SetSetting(string section, string setting, bool newValue) {
            return SetSetting(section, setting, Setting.FromBoolean(newValue));
        }

        /// <summary>
        /// Sets a profile setting, overwriting any previous setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <param name="a_iNewValue">The new INI value.</param>
        /// <returns>Returns true if set, false if not set.</returns>
        public bool SetSetting(string section, string setting, int newValue) {
            return SetSetting(section, setting, Setting.FromInteger(newValue));
        }

        /// <summary>
        /// Sets a profile setting, overwriting any previous setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <param name="a_fcsNewValue">The new INI value.</param>
        /// <returns>Returns true if set, false if not set.</returns>
        public bool SetSetting(string section, string setting, FCSSetting newValue) {
            return SetSetting(section, setting, Setting.FromString(newValue.ToString()));
        }

        /// <summary>
        /// Gets a profile setting.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sSetting">The INI setting.</param>
        /// <returns>Returns the setting as a setting object, or null if not found.</returns>
        public Setting GetSetting(string section, string setting) {
            return IniParser.GetSetting(section, setting);
        }

        /// <summary>
        /// Sets a list of string settings to the profile.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sItemName">The INI setting prefix.</param>
        /// <param name="a_sList">The list of strings.</param>
        public void SetSettingStringList(string section, string itemName, List<string> list) {
            SetSetting(section, itemName + "Count", list.Count);
            for (int i = 1; i <= list.Count; i++) {
                SetSetting(section, itemName + i.ToString(), list[i - 1]);
            }
            SaveSettings();
        }

        /// <summary>
        /// Gets a list of string settings from the profile.
        /// </summary>
        /// <param name="a_sSection">The INI section.</param>
        /// <param name="a_sItemName">The INI setting prefix.</param>
        /// <returns>The list of strings.</returns>
        public List<string> GetSettingStringList(string section, string itemName) {
            int count = GetSetting(section, itemName + "Count").ToInteger(0);
            List<string> list = new List<string>(count);
            for (int i = 1; i <= count; i++) {
                list.Add(GetSetting(section, itemName + i.ToString()).ToString());
            }
            return list;
        }

        /// <summary>Save the settings to the .fbp file.</summary>
        public void SaveSettings() {
            IniParser.SaveSettings();
        }

        /// <summary>Remove this profile.</summary>
        public void Delete() {
            IniParser.Delete();
        }

        internal frmProfile GetForm() {
            foreach (System.Windows.Forms.Form frm in System.Windows.Forms.Application.OpenForms) {
                if (frm.Text.StartsWith(ProfileName + " - "))
                    return (frmProfile) frm;
            }
            return null;
        }
    }
}
